package com.wwp.tbshop.model;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.LimitBuyActivity;
import com.wwp.tbshop.model.bean.LimitBuyBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;
import com.wwp.tbshop.util.utils.SecToHourMinSecFormat;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ${wenping} on 6/19/2017.
 */

public class LimitBuyPlvAdapter extends BaseAdapter {

    private LimitBuyActivity mLimitBuyActivity;
    private static int time;
    public CountDownTimer mCountDownTimer;

    public LimitBuyPlvAdapter(LimitBuyActivity limitBuyActivity) {
        mLimitBuyActivity = limitBuyActivity;
    }

    private List<LimitBuyBean.ProductListBean> mProductListBeanList;

    public void setProductListBeanList(List<LimitBuyBean.ProductListBean> productListBeanList) {
        mProductListBeanList = productListBeanList;
        notifyDataSetChanged();
    }

/*    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    notifyDataSetChanged();
                    break;
            }
        }
    };

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < mProductListBeanList.size(); i++) {
                LimitBuyBean.ProductListBean bean = mProductListBeanList.get(i);
                int leftTime = bean.getLeftTime();
                if (leftTime>1000){
                    bean.setLeftTime(leftTime-1);
                }
            }
            notifyDataSetChanged();
            // TODO: 6/19/2017
            handler.postDelayed(this,1000);
        }
    };*/

    @Override
    public int getCount() {
        if (mProductListBeanList != null) {
            return mProductListBeanList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mProductListBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(UiUtil.getContext()).inflate(R.layout.item_limit_buy, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setData(mProductListBeanList.get(position));
        return convertView;
    }

/*    *//**停止方法*//*
    public void stop() {
        handler.removeCallbacks(mRunnable);
    }

    *//**开始方法*//*
    public void start() {
        handler.post(mRunnable);
    }*/
    static class ViewHolder {
        @Bind(R.id.item_limit_buy_goodsiv)
        ImageView mItemLimitBuyGoodsiv;
        @Bind(R.id.item_limit_buy_name)
        TextView mItemLimitBuyName;
        @Bind(R.id.item_limit_buy_price)
        TextView mItemLimitBuyPrice;
        @Bind(R.id.item_limit_buy_marketprice)
        TextView mItemLimitBuyMarketprice;
        @Bind(R.id.item_limit_buy_lefttime)
        TextView mItemLimitBuyLefttime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setData(LimitBuyBean.ProductListBean bean) {
            Picasso.with(UiUtil.getContext())
                    .load(Constant.HOST + bean.getPic())
                    .placeholder(R.drawable.pic_default_grid)
                    .error(R.drawable.ic_error_page)
                    .into(mItemLimitBuyGoodsiv);
            mItemLimitBuyName.setText(bean.getName() + "");
            mItemLimitBuyMarketprice.setText("¥" + bean.getPrice() + "");
            mItemLimitBuyMarketprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//贯穿线
            mItemLimitBuyPrice.setTextColor(Color.RED);
            mItemLimitBuyPrice.setText("¥" + bean.getLimitPrice() + "");

            time = (bean.getLeftTime());
            String secToTime = SecToHourMinSecFormat.secToTime(time);
            mItemLimitBuyLefttime.setText(secToTime);
        }
    }

    public void startCountDown(){
        //一秒钟触发一次
        mCountDownTimer = new CountDownTimer(Integer.MAX_VALUE, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //一秒钟触发一次
                for (LimitBuyBean.ProductListBean listBean : mProductListBeanList) {
                    int leftTime = listBean.getLeftTime();
                    if (leftTime < 0) {
                        leftTime = 0;
                    } else {
                        leftTime--;
                    }
                    listBean.setLeftTime(leftTime);
                }
                notifyDataSetChanged();
            }

            @Override
            public void onFinish() {

            }
        }.start();
    }

    public void stopCountDown(){
        mCountDownTimer.cancel();
    }
}
